﻿Imports System.Data
Imports System.Data.SqlClient

Imports Logistica.Entities

Public NotInheritable Class AlmacenDO
    Inherits DataObjectBase

    ''' <summary>
    ''' Listado de los almacenes
    ''' </summary>
    Public Function Listar() As List(Of AlmacenBE)
        Dim resultado As New List(Of AlmacenBE)

        Dim procedimiento As String = "ListarAlmacenes"

        Using conexion As New SqlConnection(Me.ConnectionString)
            Dim comando As New SqlCommand(procedimiento, conexion)
            comando.CommandType = CommandType.StoredProcedure

            Try
                conexion.Open()
                Dim reader As IDataReader = comando.ExecuteReader()

                While reader.Read
                    Dim info As New AlmacenBE
                    info.Id = reader.GetInt32(reader.GetOrdinal("IdAlmacen"))
                    info.Nombre = reader.GetString(reader.GetOrdinal("Nombre"))
                    info.Descripcion = reader.GetString(reader.GetOrdinal("Descripcion"))

                    resultado.Add(info)
                End While


            Catch ex As Exception
                Throw ex

            Finally
                If conexion.State <> ConnectionState.Closed Then
                    conexion.Close()
                End If
            End Try


        End Using



        Return resultado
    End Function

    ''' <summary>
    ''' Se usa para insertar un almacen
    ''' </summary>
    Public Function Insertar(infoAlmacen As AlmacenBE) As Integer
        Dim codigoRegistro As Integer = 0
        Dim procedimiento As String = "InsertarAlmacen"

        Using conexion As New SqlConnection(Me.ConnectionString)
            Dim comando As New SqlCommand(procedimiento, conexion)
            comando.CommandType = CommandType.StoredProcedure
            comando.Parameters.Add(New SqlParameter("@Codigo", infoAlmacen.Codigo))
            comando.Parameters.Add(New SqlParameter("@Nombre", infoAlmacen.Nombre))
            comando.Parameters.Add(New SqlParameter("@Descripcion", infoAlmacen.Descripcion))

            Try
                conexion.Open()
                codigoRegistro = comando.ExecuteScalar


            Catch ex As Exception
                Throw ex

            Finally
                If conexion.State <> ConnectionState.Closed Then
                    conexion.Close()
                End If
            End Try


        End Using

        Return codigoRegistro
    End Function

    Public Sub Actualizar(infoAlmacen As AlmacenBE)
        Dim procedimiento As String = "ActualizarAlmacen"

        Using conexion As New SqlConnection(Me.ConnectionString)
            Dim comando As New SqlCommand(procedimiento, conexion)
            comando.CommandType = CommandType.StoredProcedure

            comando.Parameters.Add(New SqlParameter("@IdAlmacen", infoAlmacen.Id))
            comando.Parameters.Add(New SqlParameter("@Codigo", infoAlmacen.Codigo))
            comando.Parameters.Add(New SqlParameter("@Nombre", infoAlmacen.Nombre))
            comando.Parameters.Add(New SqlParameter("@Descripcion", infoAlmacen.Descripcion))

            Try
                conexion.Open()
                comando.ExecuteNonQuery()

            Catch ex As Exception
                Throw ex

            Finally
                If conexion.State <> ConnectionState.Closed Then
                    conexion.Close()
                End If
            End Try


        End Using
    End Sub

    Public Sub Eliminar(infoAlmacen As AlmacenBE)
        Dim procedimiento As String = "EliminarAlmacen"


        Using conexion As New SqlConnection(Me.ConnectionString)
            Dim comando As New SqlCommand(procedimiento, conexion)
            comando.CommandType = CommandType.StoredProcedure

            comando.Parameters.Add(New SqlParameter("@IdAlmacen", infoAlmacen.Id))

            Try
                conexion.Open()
                comando.ExecuteNonQuery()

            Catch ex As Exception
                Throw ex

            Finally
                If conexion.State <> ConnectionState.Closed Then
                    conexion.Close()
                End If
            End Try


        End Using
    End Sub

    Function Obtener(infoAlmacen As AlmacenBE) As AlmacenBE
        Dim resultado As New AlmacenBE

        Dim procedimiento As String = "ObtenerAlmacen"

        Using conexion As New SqlConnection(Me.ConnectionString)
            Dim comando As New SqlCommand(procedimiento, conexion)
            comando.CommandType = CommandType.StoredProcedure
            comando.Parameters.Add(New SqlParameter("@IdAlmacen", infoAlmacen.Id))

            Try
                conexion.Open()
                Dim reader = comando.ExecuteReader()

                While reader.Read
                    resultado.Id = reader.GetInt32(reader.GetOrdinal("IdAlmacen"))
                    resultado.Codigo = reader.GetString(reader.GetOrdinal("Codigo"))
                    resultado.Nombre = reader.GetString(reader.GetOrdinal("Nombre"))
                    resultado.Descripcion = reader.GetString(reader.GetOrdinal("Descripcion"))

                End While


            Catch ex As Exception
                Throw ex

            Finally
                If conexion.State <> ConnectionState.Closed Then
                    conexion.Close()
                End If
            End Try


        End Using



        Return resultado
    End Function









End Class
